package topt_12z.network;

import java.util.LinkedList;
import java.util.logging.Logger;

import topt_12z.logging.IO;
import topt_12z.statistics.SimulationStatistics;

public class NetworkBuffer<E> extends LinkedList<E> {
	private static final long serialVersionUID = 8260703244726855235L;
	private int limit;
	private Logger log;
	private SimulationStatistics stats = null;

	public NetworkBuffer(int limit) {
		try {
			log=IO.getInstance().getLogger();
		} catch (Exception e) {
			// TODO: handle exception
		}
		this.limit = limit;
		
		stats = SimulationStatistics.getInstance();
	}

	@Override
	/**
	 * add object to buffer
	 * return true if object was added, false if not
	 */
	public boolean add(E obj) {
		if (size() >= limit) {
			// queue is full, dont add - log that
			log.fine("queue full, dropping entry " + obj.toString());
			stats.incrementLostBufferFull();
			return false;
		} else {
			// there is some space in queue - add it
			super.add(obj);
			return true;
		}

	}
}
